<?php
require_once "db.php";

$cinema_name = db_escape($_POST['cinemaname']);

function my_sort($a,$b)
{
	if ($a==$b) return 0;
	  return ($a<$b)?-1:1;
}

if (db_escape($_POST['index'])=='1') {
	$program_date = date("Y/m/d");
}
else {
	$program_date = db_escape($_POST['program_date']);
}
$program_content = "<div id='cin_date_content'><div id='chosen_cinema'>".$cinema_name."</div>   ".$program_date."</div><div id='main_program'>";
$take_cinemaid = "SELECT idcinema FROM cinema WHERE cinema_name = '$cinema_name'";
$get_cinemaid = db_query($take_cinemaid);
while ($cinemaid = mysqli_fetch_assoc($get_cinemaid)) {
	$cinema_id = $cinemaid['idcinema'];
}

$movie_from_dual = "SELECT movies_idmovies FROM cinema_has_movies WHERE cinema_idcinema = '$cinema_id'";
$movie_from_dual_query = db_query($movie_from_dual);
while ($dual_row = mysqli_fetch_assoc($movie_from_dual_query)) {
		$movieid = $dual_row['movies_idmovies'];
		$movie_query_text = "SELECT movie_name FROM movies WHERE (past_present_future = '1' OR past_present_future = '4') AND idmovies = '$movieid'";
		$movie_query = db_query($movie_query_text);
		while ($movie_row = mysqli_fetch_assoc($movie_query)){
			$moviename = $movie_row['movie_name'];
			$program_content .="<div class='movie_day'><div class='movie_title'>".$moviename."</div>";
			$proj_query_text = "SELECT idprojection, proj_begin FROM projection WHERE proj_date='$program_date' AND movies_idmovies='$movieid' AND cinema_idcinema='$cinema_id'";
			$proj_query = db_query($proj_query_text);
			$date_id = array();
			$date_keys = array();
			while ($proj_row = mysqli_fetch_assoc($proj_query)){
				$date_id[strtotime($proj_row['proj_begin'])]=$proj_row['idprojection'];
				$date_keys[] = strtotime($proj_row['proj_begin']);
			}
			usort($date_keys, 'my_sort');
			foreach ($date_keys as $value) {
				$program_content .= "<div class='hour_begin'><a href='#' id='".$date_id[$value]."'>".date('H:i', $value)."</a></div>";
			}
		$program_content .= "</div>";
	}
}


$program_content .= "</div>";
echo $program_content;




